package com.kk;

import sun.util.resources.LocaleData;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * the class of Default (Template) Project
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2023-07-07
 */
public class Main {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student(1, 19, "张三", "A", true));
        students.add(new Student(1, 20, "张三", "A", true));
        students.add(new Student(1, 18, "李四", "a", false));
        students.add(new Student(1, 21, "王五", "B", true));
        students.add(new Student(1, 20, "赵六", "b", false));
        students.add(new Student(1, 20, "赵六", "F", false));
        students.add(new Student(1, 20, "赵六", "F", false));
        students.add(new Student(1, 20, "赵六", "F", false));

        // 1.中间管道 filter 过滤，终止管道 forEach 遍历
        System.out.println("// 1.中间管道 filter 过滤，终止管道 forEach 遍历");
        students.stream()
                .filter(student -> student.getAge() < 20)
                .forEach(System.out::println);

        // 2.collect 收集
        System.out.println("\n// 2.collect 收集");
        List<Student> list = students.stream()
                .filter(student -> student.getAge() <= 20)
                .collect(Collectors.toList());
        System.out.println(list);
        Set<Student> set = students.stream()
                .filter(student -> student.getAge() < 20)
                .collect(Collectors.toSet());
        System.out.println(set);

        // 3.distinct 去重
        System.out.println("\n// 3.distinct 去重");
        List<Student> studentList = students.stream()
                .distinct()
                .collect(Collectors.toList());
        System.out.println(studentList);


        List<String> ids = Arrays.asList("234","100","200","305");
        // 4.map 一对一转换.
        System.out.println("\n// 4.map 一对一转换.");
        List<Integer> results = ids.stream()
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        System.out.println(results);

        List<String> sentences = Arrays.asList(" hello world","Jia Gou Wu Dao ","a", "b");
        // 5.flatMap 一对多转换.
        System.out.println("\n// 5.flatMap 一对多转换.");
        List<String> r = sentences.stream()
                .flatMap(sentence -> Arrays.stream(sentence.split(" ")))
                .collect(Collectors.toList());
        System.out.println(r);

        // 6.peek中间管道，对stream中的每个元素逐个遍历处理，中间管道必须搭配终止管道一起使用，否则中间管道的操作不会执行。
        System.out.println("\n// 6.peek中间管道，对stream中的每个元素逐个遍历处理，中间管道必须搭配终止管道一起使用，否则中间管道的操作不会执行。");
        sentences.stream().filter(s -> {
            System.out.println(s);
            return s.contains("hello");
        }).forEach(System.out::println);
        sentences.stream()
                .peek(System.out::println);
        sentences.stream()
                .peek(System.out::println)
                .forEach(System.out::println);


        // 7.List或者数组中的值拼接到一个字符串里并以逗号分隔开, Collectors.joining
        System.out.println("\n// 7.List或者数组中的值拼接到一个字符串里并以逗号分隔开, Collectors.joining");
        String toOneString = ids.stream()
                .collect(Collectors.joining(","));
        System.out.println(toOneString);

        // 8.Collectors.averagingInt, 求集合均值
        System.out.println("\n // 8.Collectors.averagingInt, 求集合均值");
        Double collect = ids.stream()
                .map(Integer::parseInt)
                .collect(Collectors.averagingInt(value -> value));
        System.out.println(collect);

        // 9.统计信息， Collectors.summarizingInt
        System.out.println("\n // 9.统计信息， Collectors.summarizingInt");
        IntSummaryStatistics summaryStatistics = ids.stream()
                .collect(Collectors.summarizingInt(Integer::parseInt));
        System.out.println(summaryStatistics);

        // 10.Collectors.groupingBy
        System.out.println("\n// 10.Collectors.groupingBy");
        Map<Object, Map<Object, List<Student>>> collect1 = students.stream()
                .collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getName)));
        System.out.println(collect1);

        // 11.sorted排序, 默认升序，可自定义排序规则。
        System.out.println("\n// 11.sorted排序, 默认升序，可自定义排序规则.");
        System.out.println("根据sex升序");
        List<Student> collect2 = students.stream()
                .distinct()
                .sorted( Comparator.comparing(student -> student.getSex()))
                .collect(Collectors.toList());
        System.out.println(collect2);

        //但是这样有点不好，因为是先排序然后在逆序的，要两步操作。
        System.out.println("reversed, 根据sex降序，//但是这样有点不好，因为是先排序然后在逆序的，要两步操作。");
        List<Student> collect3 = students.stream()
                .distinct()
                .sorted( Comparator.comparing(Student::getSex).reversed())
                .collect(Collectors.toList());
        System.out.println(collect3);
        // reverseOrder 同 reversed 一样，需要两步操作。
        System.out.println("// reverseOrder 同 reversed 一样，需要两步操作");
        List<Student> collect4 = students.stream()
                .distinct()
                .sorted( Comparator.comparing(Student::getSex, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        System.out.println(collect4);

        System.out.println("// sex age 多级排序");
        List<Student> collect44 = students.stream()
                .distinct()
                .sorted(Comparator.comparing(Student::getSex).thenComparing(Student::getAge, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        System.out.println(collect44);

        // o1-o2 升序， o2-o1 降序
        System.out.println("\n// 在sorted中自定义排序规则.");
        List<Student> collect5 = students.stream()
                .distinct()
                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                .collect(Collectors.toList());
        System.out.println(collect5);

        // 字典顺序排序
        System.out.println("\n// 字典顺序.");
        List<Student> collect55 = students.stream()
                .distinct()
                .sorted((o1, o2) -> {
                    try {
                        String str1 = new String(o1.getName().getBytes("GB2312"), StandardCharsets.ISO_8859_1);
                        String str2 = new String(o2.getName().getBytes("GB2312"), StandardCharsets.ISO_8859_1);
                        return str1.compareTo(str2);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    return 0;
                })
                .collect(Collectors.toList());
        System.out.println(collect55);

        File file = new File("a.txt");
        File file1 = new File("b.txt");
        List<String> list1 = Files.readAllLines(Paths.get("a.txt"));
        List<String> list2 = Files.readAllLines(Paths.get("b.txt"));


        // 文件内容比较
        System.out.println("// 文件内容比较");
        List<String> collect6 = list1.stream()
                .filter(s -> list2.stream().noneMatch(s1 -> s1.trim().equals(s.trim())))
                .collect(Collectors.toList());
        System.out.println("old：" + collect6 + "已删除");

        List<String> collect7 = list2.stream()
                .filter(s -> list1.stream().noneMatch(s1 -> s1.trim().equals(s.trim())))
                .collect(Collectors.toList());
        System.out.println("new：" + collect7 + "新增");
    }
}